<template>
  <div class="app-container">
    <el-card class="box-card">
      <div
        slot="header"
        class="clearfix"
      >
        <span>编辑快速求职</span>
        <el-button
          style="float: right; padding: 0;margin-left:14px"
          type="text"
          @click="goTo('/fast/resume')"
        >
          返回
        </el-button>
        <el-button
          style="float: right; padding: 0;"
          type="text"
          @click="onSubmit('form')"
          :disabled="issubmit"
        >
          保存
        </el-button>
      </div>
      <el-form
        ref="form"
        class="common-form"
        :model="form"
        label-width="120px"
        :rules="rules"
        :inline-message="true"
      >
        <el-form-item label="姓名" prop="fullname">
          <el-input v-model="form.fullname" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="工作年限" prop="experience">
          <el-select v-model="form.experience" placeholder="请选择工作年限">
            <el-option
              v-for="(item, index) in experienceCategory"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="想找工作" prop="wantjob">
          <el-input v-model="form.wantjob" />
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" />
        </el-form-item>
        <el-form-item label="具体描述" prop="content">
          <el-input v-model="form.content" type="textarea" rows="8" />
        </el-form-item>
        <el-form-item label="有效期延长" prop="valid" >
          <el-input v-model="form.valid" style="width:100px;"/>天
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" :disabled="issubmit" @click="onSubmit('form')">保存</el-button>
          <el-button @click="goTo('/fast/resume')">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { fastGetExperience , fastResumeEdit} from '@/api/fast_resume'

export default {
  data () {
    return {
      issubmit: false,
      experienceCategory: [],
      info: [],
      form: {
        fullname: '',
        sex: '',
        experience: '',
        wantjob: '',
        telephone: '',
        content: '',
        valid: ''
      },
      rules: {
        fullname: [
          { required: true, message: '请填写姓名', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        experience: [
          { required: true, message: '请选择工作年限', trigger: 'change' }
        ],
        wantjob: [
          { required: true, message: '请填写想找工作', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '请填写联系电话', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请填写具体描述', trigger: 'blur' }
        ],
        valid: [
          { required: true, message: '请选择有效期', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
  },
  created() {
    this.fetchInfo()
  },
  methods: {
    fetchInfo () {
      fastGetExperience().then(response => {
        this.experienceCategory = { ...response.data }
      })
      fastResumeEdit({id: this.$route.query.id}, 'get').then(respons => {
        this.form = respons.data.info
        this.form.valid = 0
      })
    },
    goTo (target) {
      this.$router.push(target)
    },
    onSubmit(formName) {
      const that = this
      that.issubmit = true
      const insertData = { ...this.form }
      this.$refs[formName].validate(valid => {
        if (valid) {
          fastResumeEdit(insertData).then(response => {
            this.$message.success(response.message)
            setTimeout(function() {
              that.$router.push('/fast/resume')
            }, 1500)
            return true
          })
            .catch(() => {
              that.issubmit = false
              return false
            })
        } else {
          that.issubmit = false
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input, .el-select, .el-cascader, .el-textarea {
  width: 450px;
}
</style>
